﻿PagesScripts.forumPosts = {};

var myScroll; /* iScroll */

PagesScripts.forumPosts.initHandlers = function() {
	if (!HTTPEvents.hasEventListener("onForumPosts")) {
		HTTPEvents.addEventListener("onForumPosts", PagesScripts.forumPosts.onForumPosts);
	}

	if (!HTTPEvents.hasEventListener("onPrePostPageLoaded")) {
		HTTPEvents.addEventListener("onPrePostPageLoaded", PagesScripts.post.onPrePostPageLoaded);
	}

	if (!HTTPEvents.hasEventListener("onPostSent")) {
		HTTPEvents.addEventListener("onPostSent", PagesScripts.post.onPostSent);
	}
	
	PagesScripts.forumPosts.page = 1;
	
	$("#postNewTitle").live("keyup", function() {
		PagesScripts.post.setPostState();
	});
	
	$("#posts_list").bind("tap", function(event) {
		Config.SetCurrentPost(event);
		PageManager.changePage("../post");
	});


    /* iScroll code */
    var pullDownEl, pullDownOffset;

    pullDownEl = document.getElementById('pullDown');
    pullDownOffset = 51; // should be "pullDownEl.offsetHeight", but there is a bug.

    myScroll = new iScroll('wrapper', {
        useTransition: true,
        topOffset: pullDownOffset,
        onRefresh: function () {
            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = 'משוך לרענון...';
            }
        },
        onScrollMove: function () {
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = 'שחרר לרענון...';
                this.minScrollY = 0;
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = 'משוך לרענון...';
                this.minScrollY = -pullDownOffset;
            }
        },
        onScrollEnd: function () {
            if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = 'טוען אשכולות, אנא המתן...';
                PagesScripts.forumPosts.loadForumPosts(1);
            }
        }
    });
    
    if (Utils.isAndroid()) {
    	$("#wrapper").css("bottom", 0);
    	myScroll.refresh();
    };
}

PagesScripts.forumPosts.incPage = function(value) {
	PagesScripts.forumPosts.page = PagesScripts.forumPosts.page + value;
	PagesScripts.forumPosts.loadForumPosts();
}

PagesScripts.forumPosts.loadForumPosts = function(page) {
	if (page != null)
		PagesScripts.forumPosts.page = page;    
    else
        $.mobile.loading('show', {text: 'טוען אשכולות...'});
	$("#forum_name_title", $.mobile.activePage).html(Config.GetCurrentForum().title);
	if (Config.GetPostsList() == null || Config.GetPostsFromCache() != true) {
		HTTPManager.GetForumPosts(Config.GetCurrentForum().name, PagesScripts.forumPosts.page);
	}
	else {
		PagesScripts.forumPosts.populatePostsList(Config.GetPostsList());
		Config.SetPostsFromCache(false);
	}
}

PagesScripts.forumPosts.onForumPosts = function(event) {
	$("#posts_list", $.mobile.activePage).html("");
	if (event.data && event.data.success != null && event.data.success === true && event.data.posts && event.data.posts.length > 0) {
		PagesScripts.forumPosts.populatePostsList(event.data);
		Config.SetPostsList(event.data);
        myScroll.refresh(); // Refresh iScroll
	}
}

PagesScripts.forumPosts.populatePostsList = function(source) {
	for (var i=0;i<source.posts.length;i++) {
		$("#forumPostItem").tmpl(source.posts[i], {
			getPostColor:function() {
				if (this.data.anchor) {
					if (this.data.locked)
						return "#009933";
					else
						return "#000099";
				}
				else
					return "#000000";
			}
		}).appendTo("#posts_list", $.mobile.activePage);
	}
	$("#posts_list", $.mobile.activePage).listview("refresh");
	$("#forumPageNum", $.mobile.activePage).html(PagesScripts.forumPosts.page + " מתוך " + source.numPages);
	$("#forumPagingContainer").show();
	$("#forumPagePrev", $.mobile.activePage).css("display", (PagesScripts.forumPosts.page == 1) ? "none" : "block");
	$("#forumPageNext", $.mobile.activePage).css("display", (PagesScripts.forumPosts.page == source.numPages) ? "none" : "block");
    $.mobile.loading('hide');
	myScroll.refresh();
	myScroll.scrollTo(0, 0, 0);
}